<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="200"
			label-align="left">
			<u-form-item label="借款单号" v-if="judgeShow('ti002')">
				<u-input v-model="dataForm.ti002" disabled placeholder="提交保存后系统自动生成"></u-input>
			</u-form-item>
			<u-form-item label="借款金额" v-if="judgeShow('ti004')">
				<u-input v-model="dataForm.ti004" type='number' :disabled="judgeWrite('ti004')"></u-input>
			</u-form-item>
			<u-form-item label="借款日期" v-if="judgeShow('ti005')">
				<lys-date v-if="showte005" type="1" @cancleTime="showte005 = false"
					@submitTime="submitte005"></lys-date>
				<view style="display: flex;justify-content: space-between;width: 100%;">
					<view>
						{{dataForm.ti005}}
					</view>
					<view>
						<u-icon name="arrow-down-fill" color='#ccc' @click="showte005=true"></u-icon>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="归还日期" v-if="judgeShow('ti006')">
				<lys-date v-if="showte006" type="1" @cancleTime="showte006 = false"
					@submitTime="submitte006"></lys-date>
				<view style="display: flex;justify-content: space-between;width: 100%;">
					<view>
						{{dataForm.ti006}}
					</view>
					<view>
						<u-icon name="arrow-down-fill" color='#ccc' @click="showte006=true"></u-icon>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="还款金额" v-if="judgeShow('ti010')">
				<u-input v-model="dataForm.ti010" disabled></u-input>
			</u-form-item>
			<u-form-item label="收款人姓名" v-if="judgeShow('ti008')">
				<jnpf-user-select v-model="dataForm.ti008" @input="changeName" />
			</u-form-item>
			<u-form-item label="部门" v-if="judgeShow('ti008')">
				{{dataForm.ti014}}
			</u-form-item>
			<u-form-item label="借款前余额" v-if="judgeShow('ti015')">
				<u-input v-model="dataForm.ti015" disabled></u-input>
			</u-form-item>
			<u-form-item label="借款额度" v-if="judgeShow('ti016')">
				<u-input v-model="dataForm.ti016" disabled></u-input>
			</u-form-item>
			<u-form-item label="剩余额度" v-if="judgeShow('ti017')">
				<u-input v-model="dataForm.ti017" disabled></u-input>
			</u-form-item>
			<u-form-item label="借款事由" v-if="judgeShow('ti003')">
				<u-input v-model="dataForm.ti003" :disabled="judgeWrite('ti003')"></u-input>
			</u-form-item>
			<u-form-item label="备注" v-if="judgeShow('ti011')">
				<u-input v-model="dataForm.ti011" :disabled="judgeWrite('ti011')"></u-input>
			</u-form-item>
			<u-form-item label="附件" type="textarea" v-if="judgeShow('ti012')">
				<jnpf-file v-model="dataForm.ti012" :disabled="judgeWrite('ti012')" />
			</u-form-item>
		</u-form>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import comMixin from "../mixin";
	import {
		UserSettingInfo
	} from "@/api/user"
	import {
		getGoodsList,
		getCustomerList,
		Info,
	} from "@/api/apply/order";
	import {
		postOasti,
		putOaste,
		BomcbListInit,
		getOastiBudgetNo,
		getOastdList
	} from "@/api/workFlow/flowBefore";
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo,
		getUserAll
	} from "@/api/common";
	import request from '@/utils/request'
	export default {
		mixins: [comMixin],
		watch: {},
		data() {
			return {
				show: false,
				popupshow: false,
				showtf003: false,
				showte005: false,
				showte006: false,
				showte008: false,
				showte009: false,
				productionQueueTitle: [{
						title: "序号",
						key: "sid",
						width: '80',
					},
					{
						title: "费用类型编号",
						key: "td001",
						width: '80',
					},
					{
						title: "费用类型名称",
						key: "td002",
						width: '80',
					},

				],
				dataForm: {
					flowId: "",
					ti002: "",
					ti003: "",
					ti004: 0,
					ti005: "",
					ti006: "",
					ti008: "",
					ti010: 0,
					ti011: "",
					ti014: "",
					ti015: "",
					ti016: "",
					ti017: "",
					ti012: [],
					flowState: 0 //提交0 保存1
				},
				id: "",
				tableList: [],
				tk003: '',
				tk003Name: '',
				index: '',

				setting: {},
			};
		},
		methods: {
			getUserInfo() {
				UserSettingInfo().then((res) => {
					this.dataForm.ti008 = res.data.id; //用户
					this.dataForm.ti013 = res.data.organizeId; //部门
					this.dataForm.ti014 = res.data.department; //部门名称
					this.getBorrowMoneyInfo()
				})
			},
			//根据用户获取员工姓名的借款额度，借款余额
			getBorrowMoneyInfo() {
				var queryParam = {
					ti008: this.dataForm.ti008,
				};
				request({
					url: "/api/SubDev/Oasti/getBorrowMoneyInfo",
					method: "get",
					data: queryParam,
				}).then((res) => {
					this.dataForm.ti015 = res.data.beforeLoanBalance;
					this.dataForm.ti016 = res.data.mv080;
					this.dataForm.ti017 = res.data.mv081;
				});
			},
			changeName(id) {
				getUserAll().then(res => {
					let item = res.data.filter(o => o.id === id)[0]
					this.dataForm.ti013 = item.departmentId;
					this.dataForm.ti014 = item.department;
					this.getBorrowMoneyInfo()
				})
			},
			judgeShow(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return true;
				let arr = this.setting.formOperates.filter((o) => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return item.read;
			},
			judgeWrite(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return false;
				let arr = this.setting.formOperates.filter((o) => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return !item.write;
			},
			submitte005(time, type, timeInt) {
				let ti005 = time.split("-");
				this.dataForm.ti005 = ti005[0] + ti005[1] + ti005[2]
				this.showte005 = false;
			},
			submitte006(time, type, timeInt) {
				let ti006 = time.split("-");
				this.dataForm.ti006 = ti006[0] + ti006[1] + ti006[2]
				this.showte006 = false;
			},
			getInfoFn(data) {
				this.setting = data
				this.dataForm.flowId = data.flowId
				getOastiBudgetNo().then((res) => {
					if (res.code == 200) {
						this.dataForm.ti005 = this.jnpf.getCurrentDate('YYYYMMDD');
						this.dataForm.ti006 = this.jnpf.getCurrentDate('YYYYMMDD');
						this.getUserInfo()
					}
				});
			},
			selfSubmit(eventType) {
				//限制借款金额不能超过剩余额度
				if (this.dataForm.ti004 > this.dataForm.ti017) {
					this.dataForm.ti004 = 0;
					return this.$refs.uToast.show({
						title: "借款金额不能超过剩余额度",
						type: "error",
					});
				}
				//限制借款金额不能超过剩余额度
				if (this.jnpf.isEmpty(this.dataForm.ti004) || this.dataForm.ti004 == 0) {
					return this.$refs.uToast.show({
						title: "借款金额不能为空/0",
						type: "error",
					});
				}
				this.dataForm.status = eventType === "submit" ? 0 : 1;
				uni.showModal({
					title: "提示",
					content: "您确定要提交当前流程吗？",
					success: (res) => {
						if (res.confirm) {
							this.HandleSubmit(eventType);
						}
					},
				});
			},
			HandleSubmit(eventType) {
				this.dataForm.flowState = eventType == "submit" ? 0 : 1;
				console.log('this.dataForm', this.dataForm);
				postOasti(this.dataForm).then((res) => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit("refresh");
								uni.navigateBack();
							}, 1500);
						},
					});

				});
			},

		},
	};
</script>